<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            outline: none;
            list-style: none;
        }

        .seachform {
            width: 500px;
            height: 50px;
            margin: 50px auto;
            position: relative;
            border: 1px solid #ccc;
            display: flex;
            border-radius: 50px;
        }

        .search {
            flex: 1;
            border-radius: 50px 0 0 50px;
            border: none;
            font-size: 18px;
            text-indent: 34px;
        }

        .searchbtn {
            width: 80px;
            background: rgb(155, 150, 150);
            border-radius: 0 50px 50px 0;
            cursor: pointer;
            color: #fff;
        }

        .searchbtn:hover {
            background: #ccc;
        }

        .layer {
            position: absolute;
            width: calc(100% - 102px);
            background: #fff;
            top: 50px;
            left: 20px;
            border: 1px solid #ccc;
            display: none;
        }

        .layer>li {
            padding: 10px;
            cursor: pointer;
        }

        .layer>li:hover {
            background: #ccc;
        }
    </style>
</head>

<body>
    <div class="seachform">
        <input type="text" class="search" placeholder="请输入搜索内容">
        <button class="searchbtn">搜索</button>
        <ul class="layer">

        </ul>
        <script id="temp" type="text/html">
        {{each $data item i}}
            <li>{{item}}</li>
        {{/each}}
        </script>
    </div>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
    <script src="artTemplate.js"></script>
    <script src="https://cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
    <script>
        $(() => {
            var arr = []
            if ($.cookie("remember")) {
                arr = JSON.parse($.cookie("remember"))
                $(".layer").html(template("temp", arr))
            }

            $(".search").focus(function () {
                if (arr.length != 0) {
                    $(".layer").show()
                }
            }).blur(function () {
                setTimeout(() => {
                    $(".layer").hide()
                }, 150);
            }).on("input", function () {
                var newArr = arr.filter(item => item.startsWith($(".search").val()))
                if (newArr.length != 0) {
                    $(".layer").html(template("temp", newArr))
                }
            })
            //定义关键字
            var keywords = ""
            $(".layer").on("mouseenter", "li", function () {
                keywords = $(".search").val()
                $(".search").val($(this).text())
            }).on("mouseleave", "li", function () {
                $(".search").val(keywords)
            }).on("click", "li", function () {
                keywords = $(this).text()
            })
            //设置回车搜索
            $(window).keydown(e => {
                if (e.keyCode === 13) showLi()
            })
            //设置按键搜索
            $(".searchbtn").click(function () {
                showLi()
            })
            //搜索记录呈现及转存cookie
            var showLi = () => {
                keywords = $(".search").val()
                if (keywords && !arr.includes(keywords)) {
                    arr.push(keywords)
                    //设置所搜记录长度为5
                    console.log(arr.length)
                    if (arr.length > 5) arr.shift()
                    console.log(arr)
                    $.cookie("remember", JSON.stringify(arr), {
                        expires: 7
                    })
                }
            }
        })
    </script>
</body>

</html>